package com.cxh.rmsq.ui.activity.self

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ThreadUtils
import com.cxh.rmsq.R
import com.cxh.rmsq.databinding.ActivityInviteBinding
import com.cxh.rmsq.db.model.MineBean
import com.cxh.rmsq.im.IMManager
import com.cxh.rmsq.model.Resource2
import com.cxh.rmsq.ui.BaseActivity
import com.cxh.rmsq.utils.MyUtils
import com.cxh.rmsq.utils.ToastUtils
import com.cxh.rmsq.utils.dialog.SaveQrcodePopup
import com.cxh.rmsq.utils.dialog.SharePopup
import com.cxh.rmsq.viewmodel.UserInfoViewModel
import com.lxj.xpopup.XPopup
import java.io.IOException

class InviteActivity : BaseActivity() {
    private lateinit var binding: ActivityInviteBinding
    private var userInfoViewModel: UserInfoViewModel? = null
    private var locationId = ""
    private var strUrl = ""
    private var name = ""
    private var avator = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityInviteBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.clToolbar.setPadding(0, 0, 0, 0)
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.transparent))
        BarUtils.setStatusBarLightMode(this, true)

        locationId = intent.getStringExtra("locationId").toString()
        initData()

        binding.ivBack.setOnClickListener {
            finish()
        }
        binding.tvInviteList.setOnClickListener {
            startActivity(Intent(this, InviteListActivity::class.java))
        }
        binding.stvShare.setOnClickListener {
            XPopup.Builder(this)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(
                    SharePopup(this,
                        avator,
                        name, strUrl,
                        object : SharePopup.onClick {
                            override fun click(
                                s: Int,
                                s1: String
                            ) {
                                if (s == 6) {
                                    popSave()
                                }
                            }
                        })
                )
                .show()
        }
        binding.stvSave.setOnClickListener {
            popSave()
        }
    }

    private fun popSave() {
        XPopup.Builder(this)
            .dismissOnTouchOutside(false)
            .dismissOnBackPressed(false)
            .asCustom(
                SaveQrcodePopup(this,
                    avator,
                    name, strUrl,
                    object : SaveQrcodePopup.onClick {
                        override fun click(
                            s: Bitmap,
                            s1: String
                        ) {
                            Thread {
                                try {
                                    // 保存到本地
                                    MyUtils.saveBitmapToGallery(
                                        this@InviteActivity,
                                        s,
                                        "my_image_" + System.currentTimeMillis() + ".jpg"
                                    )

                                    // 提示成功
                                    ThreadUtils.runOnUiThread {
                                        Toast.makeText(
                                            this@InviteActivity,
                                            "图片已保存",
                                            Toast.LENGTH_SHORT
                                        ).show()
                                    }
                                } catch (e: IOException) {
                                    e.printStackTrace()
                                    ThreadUtils.runOnUiThread {
                                        Toast.makeText(
                                            this@InviteActivity,
                                            "下载失败",
                                            Toast.LENGTH_SHORT
                                        ).show()
                                    }
                                }
                            }.start()

                        }
                    })
            )
            .show()
    }

    private fun initData() {
        userInfoViewModel = ViewModelProviders.of(this).get(UserInfoViewModel::class.java)

        userInfoViewModel!!.requestUserSelfInfo(IMManager.getInstance().currentId)
        userInfoViewModel!!.getUserSelfInfo()
            .observe(this, object : Observer<Resource2<MineBean?>> {
                override fun onChanged(it: Resource2<MineBean?>) {
                    if (it.data != null) {
                        name = it.data.nickname.toString()
                        avator = it.data.avatar.toString()
                        if (!TextUtils.isEmpty(it.data.inviteCode)) {
                            strUrl =
                                "https://www.renmaishequ.com/im/login-mobile?inviteByCode=" + it.data.inviteCode + "&locationId=" + locationId
                            binding.ivCode.setImageBitmap(MyUtils.generateQRCode(strUrl, 800));
                        } else {
                            ToastUtils.showToast("二维码生成失败！")
                            finish()
                        }

                    }
                }
            })
    }

}